/*********************************************************************************
 *      Copyright:  (C) 2019 Wu Yujun<540726307@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  mosq_demo.c
 *    Description:  This file use mosquitto library demo mqtt client
 *                 
 *        Version:  1.0.0(2019年07月22日)
 *         Author:  Wu Yujun <540726307@qq.com>
 *      ChangeLog:  1, Release initial version on "2019年07月22日 11时01分32秒"
 *                 
 ********************************************************************************/
#include "iniparser.h"
#include "mosq_conf.h"


void set_mqtt_conf(char *ini_path,char *host,int port,char *id,char *user,char *passwd,char *topic) 
{
    char    _mqtt_port[16] ;
    char    *mqtt_port ;

    if(port)
    {
        snprintf(_mqtt_port, sizeof(_mqtt_port), "%d", port) ;
        mqtt_port = _mqtt_port ; 
    }
    else{
        mqtt_port = NULL ;
    }
    set_conf(ini_path,host,"mqtt_server_addr:host") ;
    set_conf(ini_path,mqtt_port,"mqtt_server_addr:port") ;
    set_conf(ini_path,id,"client_id:id") ;
    set_conf(ini_path,user,"user_passwd:username") ;
    set_conf(ini_path,passwd,"username:passwd") ;

    set_conf(ini_path,topic,"sub_topic:topic") ;
    set_conf(ini_path,topic,"pub_topic:topic") ;

}

void set_conf(char *ini_path, char *value, const char * entry) 
{
    int                 rv = 0;
    dictionary          *ini = NULL;
    FILE                *fp;
    
    if(!ini_path || !value || !entry)
    {
        return ;
    }
    printf("Ini file%s,set entry %s value=%s\n",ini_path, entry, value) ;


    ini = iniparser_load(ini_path); //Load ini file get mqtt conf 
    if ( ini == NULL ) {
        printf("iniparser_load error!\n");
        return ;
    }

    if(iniparser_find_entry(ini,entry)){
        printf("the value is: %s\r\n", iniparser_getstring(ini, entry, ""));
    }else{
        printf("the entry not exist...\r\n");
        return ;
    }

    rv = iniparser_set(ini, entry,value) ;
    if(rv != 0)
    {
        printf("iniparser_set %s=%s error\n",entry,value ) ;
        goto cleanup ;
    }
    fp = fopen(ini_path, "w");
    iniparser_dump_ini(ini, fp);
    fclose(fp);

cleanup:
    iniparser_freedict(ini); //Relaese ini file
}



int gain_mqtt_conf(char *ini_path,st_mqtt *mqtt, int type)
{

    dictionary          *ini = NULL;
    const char          *hostname ;
    int                 port ;
    const char          *username ;
    const char          *passwd ;
    const char          *clientid ;
    const char          *topic ;

    const char          *method ;
    const char          *json_id ;
    const char          *identifier ;
    const char          *version ;


    if(!ini_path || !mqtt)
    {
        printf("invail input parameter in %s\n", __FUNCTION__) ;
        return -1 ;
    }

    ini = iniparser_load(ini_path); //Load ini file get mqtt conf 
    if ( ini == NULL ) {
        printf("iniparser_load error!\n");
        return -2;
    }
    hostname = iniparser_getstring(ini, "mqtt_server_addr:host", DEFAULT_HOSTNAME); 
    port = iniparser_getint(ini, "mqtt_server_addr:port", DEFAULT_PORT);

    username = iniparser_getstring(ini, "user_passwd:username", DEFAULT_USERNAME);
    passwd = iniparser_getstring(ini, "user_passwd:passwd", DEFAULT_PASSWD);

    clientid = iniparser_getstring(ini, "client_id:id", DEFAULT_CLIENTID);

    identifier = iniparser_getstring(ini,"json:identifier", DEFAULT_IDENTIFIER) ;

    if(type == TYPE_SUB)
        topic = iniparser_getstring(ini, "sub_topic:topic", DEFAULT_SUBTOPIC) ;
    else if(type == TYPE_PUB)
    {
        topic = iniparser_getstring(ini,"pub_topic:topic", DEFAULT_PUBTOPIC) ;
        
        method = iniparser_getstring(ini,"json:method", DEFAULT_METHOD) ;
        json_id = iniparser_getstring(ini,"json:id", DEFAULT_JSONID) ;
        version = iniparser_getstring(ini,"json:version", DEFAULT_VERSION) ;

    }
    strncpy(mqtt->hostname, hostname,BUF_SIZE) ;
    mqtt->port = port ;

    strncpy(mqtt->username, username,BUF_SIZE) ;
    strncpy(mqtt->passwd, passwd,BUF_SIZE) ;

    strncpy(mqtt->clientid, clientid,BUF_SIZE) ;

    strncpy(mqtt->topic, topic,BUF_SIZE) ;


    strncpy(mqtt->identifier, identifier,BUF_SIZE) ;
    if(type == TYPE_PUB)
    {
        strncpy(mqtt->method, method,BUF_SIZE) ;
        strncpy(mqtt->json_id, json_id,BUF_SIZE) ;
        strncpy(mqtt->version, version,BUF_SIZE) ;
    }

#if 0
    printf(" MQTT hostname[%s]:%d,\n clientid:%s,\n username:%s, passwd:%s,\n topic:%s\n",
            mqtt->hostname, mqtt->port, mqtt->clientid, mqtt->username, mqtt->passwd, mqtt->topic);
#endif

    iniparser_freedict(ini); //Relaese ini file
    return 0 ;
}
